/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.verify.constraint;

import picounit.util.Equal;
import picounit.verify.constraint.EqualUtil;

public class Delta implements Comparable<Delta>, Equal<Delta> {
	private final Number value;
	private final double delta;

	public Delta(Number value, Number delta) {
		this.value = value;
		this.delta = delta.doubleValue();
	}

	public double differenceTo(double doubleValue) {
		return doubleValue - value.doubleValue();
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Comparable methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	public int compareTo(Delta delta) {
		double difference = delta.differenceTo(value.doubleValue());

		return Math.abs(difference) < this.delta ? 0 : difference < 0 ? -1 : 1;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Equal methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	public boolean equalTo(Delta other) {
		return Math.abs(other.differenceTo(value.doubleValue())) < delta;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Object methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	@Override
	public boolean equals(Object object) {
		return EqualUtil.isEqual(this, object);
	}
}
